<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>实时聊天应用 - 登录</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#3B82F6',
                        secondary: '#10B981',
                        dark: '#1E293B',
                        light: '#F8FAFC'
                    },
                    fontFamily: {
                        sans: ['Inter', 'system-ui', 'sans-serif'],
                    },
                }
            }
        }
    </script>
</head>
<body class="bg-gray-100 font-sans text-gray-800">
<!-- 登录页面 -->
<div id="login-page" class="min-h-screen flex items-center justify-center bg-gradient-to-br from-blue-50 to-indigo-100 p-4">
    <div class="w-full max-w-md">
        <div class="bg-white rounded-xl shadow shadow-lg overflow-hidden transform">
            <div class="p-6 sm:p-8">
                <div class="text-center mb-8">
                    <h2 class="text-[clamp(1.5rem,3vw,2rem)] font-bold text-gray-900">
                        实时聊天应用
                    </h2>
                    <p class="mt-2 text-gray-500">请登录以继续</p>
                </div>

                <form id="login-form" class="space-y-6">
                    <div>
                        <label for="name" class="block text-sm font-medium text-gray-700 mb-1">用户名</label>
                        <div class="relative">
                            <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                                <i class="fa fa-user text-gray-400"></i>
                            </div>
                            <input type="text" id="name" name="name" required
                                   class="pl-10 block w-full rounded-lg border-gray-300 shadow-sm border focus:border-primary focus:ring focus:ring-primary/20 transition-all duration-200 px-4 py-2.5"
                                   placeholder="请输入用户名">
                        </div>
                    </div>

                    <div>
                        <label for="password" class="block text-sm font-medium text-gray-700 mb-1">密码</label>
                        <div class="relative">
                            <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                                <i class="fa fa-lock text-gray-400"></i>
                            </div>
                            <input type="password" id="password" name="password" required
                                   class="pl-10 block w-full rounded-lg border-gray-300 shadow-sm border focus:border-primary focus:ring focus:ring-primary/20 transition-all duration-200 px-4 py-2.5"
                                   placeholder="请输入密码">
                        </div>
                    </div>

                    <div>
                        <button type="submit"
                                class="w-full flex justify-center py-2.5 px-4 border border-transparent rounded-lg shadow-sm text-sm font-medium text-white bg-primary hover:bg-primary/90 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary transition-all duration-200 transform hover:scale-[1.02] active:scale-[0.98]">
                            登录
                        </button>
                    </div>
                </form>

                <div id="login-error" class="mt-4 text-center text-red-500 text-sm hidden">
                    用户名或密码错误，请重试
                </div>

                <div class="mt-6 text-center">
                    <p class="text-gray-600 text-sm">
                        还没有账号？ <button id="show-register-btn" class="text-primary hover:text-primary/80 font-medium">立即注册</button>
                    </p>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    // 全局变量
    const BACK_ADDRESS_PORT = '47.121.204.23:8081';
    let token = localStorage.getItem('token');

    // DOM元素
    const loginForm = document.getElementById('login-form');
    const loginError = document.getElementById('login-error');
    const showRegisterBtn = document.getElementById('show-register-btn');

    // 页面初始化
    document.addEventListener('DOMContentLoaded', () => {
        // 检查是否已有token，直接跳转到用户列表
        if (token) {
            window.location.href = 'userList.html';
        }

        // 事件监听
        loginForm.addEventListener('submit', handleLogin);
        showRegisterBtn.addEventListener('click', () => {
            window.location.href = 'register.html';
        });
    });

    // 登录处理
    async function handleLogin(e) {
        e.preventDefault();

        const name = document.getElementById('name').value;
        const password = document.getElementById('password').value;

        try {
            const response = await fetch('http://' + BACK_ADDRESS_PORT + '/user/loginByNameAndPassword', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ name, password })
            });

            const result = await response.json();

            if (response.ok && result.data && result.data.token) {
                // 保存token和用户ID
                localStorage.setItem('token', result.data.token);
                localStorage.setItem('currentUserId', result.data.userId);
                localStorage.setItem('currentUserName', name);

                // 跳转到用户列表页面
                window.location.href = 'userList.html';
            } else {
                // 显示错误信息
                loginError.textContent = result.message || '登录失败，请重试';
                loginError.classList.remove('hidden');
            }
        } catch (error) {
            console.error('登录失败:', error);
            loginError.textContent = '网络错误，请重试';
            loginError.classList.remove('hidden');
        }
    }
</script>
</body>
</html>